package com.crm.system.controller.memberManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.memberManager.CustomerMemberCardOld;
import com.crm.model.qo.memberManager.MemberShipCardQO;
import com.crm.model.vo.contractReviewManager.ContractReviewFormVO;
import com.crm.model.vo.memberManager.MemberCardOldVO;
import com.crm.model.vo.memberManager.MemberShipCardListVO;
import com.crm.model.vo.systemManager.*;
import com.crm.system.service.cluePoolManager.ExcelUtil;
import com.crm.system.service.memberManager.MemberShipCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * 会员管理-会员卡管理
 * @author luojie
 * @date 2021/4/8 13:39
 */
@RestController
@RequestMapping("/memberShipCard")
@Slf4j
public class MemberShipCardController {

    @Autowired
    private MemberShipCardService memberShipCardService;

    /***
     * @Description: 查询会员卡列表
     * @Param:
     * @Author: luojie
     * @Date: 2021/4/8 13:41
     */
    @PostMapping("/selectMemberShipCardList")
    public DataResponse selectMemberShipCardList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                 @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                 @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                 @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                 MemberShipCardQO memberShipCardQO//查询条件
    ){
        DhPageInfo<MemberShipCardListVO> contractReviewFormVODhPageInfo = memberShipCardService.selectMemberShipCardList(memberShipCardQO,pageIndex,pageSize,sortName,sortOrder);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),contractReviewFormVODhPageInfo);
    }

    /**
     * 导入会员卡信息
     * @param request
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/importMembershipCard")
    public DataResponse importMembershipCard(HttpServletRequest request,
                                      @RequestParam MultipartFile file,
                                      @RequestParam String companyId
    ) throws IOException {
        JSONObject jsonObject = new JSONObject();
        try {
            ExcelUtil excelUtil = new ExcelUtil();
            List<MemberShipCardImportVO> list = excelUtil.excelImport(file.getInputStream(), MemberShipCardImportVO.class);//1.读取前端的excel文件并转换成list集合
            if (list == null || list.size() == 0) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
            jsonObject = memberShipCardService.importMembershipCard(list,companyId);

        } catch (Exception e) {
            log.error("memberShipCard importMembershipCard:", e);
        }
        //返回导入数量、重复数量
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject);
    }

    /**
     * 补全会员卡数据
     * @param request
     * @param file
     * @return
     * @throws IOException
     */
    /*@PostMapping("/importCard")
    public DataResponse importCard(HttpServletRequest request,
                                      @RequestParam MultipartFile file
    ) throws IOException {
        JSONObject jsonObject = new JSONObject();
        try {
            ExcelUtil excelUtil = new ExcelUtil();
            List<CardVO> list = excelUtil.excelImport(file.getInputStream(), CardVO.class);//1.读取前端的excel文件并转换成list集合
            if (list == null || list.size() == 0) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
            jsonObject = memberShipCardService.importCard(list);
        } catch (Exception e) {
            log.error("contact importContact:", e);
        }
        //返回导入数量、重复数量
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject);
    }*/

    /**
     * 导入单独会员卡信息
     * @param request
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/importAloneMembershipCard")
    public DataResponse importAloneMembershipCard(HttpServletRequest request,
                                             @RequestParam MultipartFile file
    ) throws IOException {
        JSONObject jsonObject = new JSONObject();
        try {
            ExcelUtil excelUtil = new ExcelUtil();
            List<AloneMemberShipCardImportVO> list = excelUtil.excelImport(file.getInputStream(), AloneMemberShipCardImportVO.class);//1.读取前端的excel文件并转换成list集合
            if (list == null || list.size() == 0) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
            jsonObject = memberShipCardService.importAloneMembershipCard(list);

        } catch (Exception e) {
            log.error("memberShipCard importMembershipCard:", e);
        }
        //返回导入数量、重复数量
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject);
    }



    /**
     * 导入税法直通车信息
     * @param request
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/importTaxCard")
    public DataResponse importTaxCard(HttpServletRequest request,
                                             @RequestParam MultipartFile file,
                                             @RequestParam String companyId
    ) throws IOException {
        JSONObject jsonObject = new JSONObject();
        try {
            ExcelUtil excelUtil = new ExcelUtil();
            List<TaxCardImportVO> list = excelUtil.excelImport(file.getInputStream(), TaxCardImportVO.class);//1.读取前端的excel文件并转换成list集合
            if (list == null || list.size() == 0) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
            jsonObject = memberShipCardService.importTaxCard(list,companyId);

        } catch (Exception e) {
            log.error("memberShipCard importTaxCard:", e);
        }
        //返回导入数量、重复数量
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject);
    }

    /***
     * @Description: 查询旧系统会员卡列表
     * @Param:
     * @Author: luojie
     * @Date: 2021/4/8 13:41
     */
    @PostMapping("/selectMemberCardOldList")
    public DataResponse selectMemberCardOldList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                 @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                 @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                 @RequestParam(value="sortOrder",required = false) String sortOrder,
                                                 MemberShipCardQO memberShipCardQO
    ){
        DhPageInfo<MemberCardOldVO> list = memberShipCardService.selectMemberCardOldList(pageIndex,pageSize,sortName,sortOrder,memberShipCardQO);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }

    /***
     * @Description: 查询旧系统公司名称
     * @Param:
     * @Author: luojie
     * @Date: 2021/4/8 13:41
     */
    @PostMapping("/getCompanyList")
    public DataResponse getCompanyList(){
        List<MemberCardOldVO> list = memberShipCardService.getCompanyList();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }

    /**
     * 导入客户充值信息
     * @param request
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/importCustomerPay")
    public DataResponse importCustomerPay(HttpServletRequest request,
                                             @RequestParam MultipartFile file,@RequestParam String companyId
    ) throws IOException {
        JSONObject jsonObject = new JSONObject();
        try {
            ExcelUtil excelUtil = new ExcelUtil();
            List<CustomerPayImportVO> list = excelUtil.excelImport(file.getInputStream(), CustomerPayImportVO.class);//1.读取前端的excel文件并转换成list集合
            if (list == null || list.size() == 0) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
            jsonObject = memberShipCardService.importCustomerPay(list);

        } catch (Exception e) {
            log.error("memberShipCard importCustomerPay:", e);
        }
        //返回导入数量、重复数量
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject);
    }

    /**
     * 删除充值码
     * @param request
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/updateRechargeCodeDelete")
    public DataResponse updateRechargeCodeDelete(HttpServletRequest request,
                                          @RequestParam MultipartFile file) throws IOException {
        JSONObject jsonObject = new JSONObject();
        try {
            ExcelUtil excelUtil = new ExcelUtil();
            List<RechargeCodeImportVO> list = excelUtil.excelImport(file.getInputStream(), RechargeCodeImportVO.class);//1.读取前端的excel文件并转换成list集合
            if (list == null || list.size() == 0) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
            memberShipCardService.updateRechargeCodeDelete(list);

        } catch (Exception e) {
            log.error("memberShipCard updateRechargeCodeDelete:", e);
        }
        //返回导入数量、重复数量
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject);
    }

    /**
     * 删除兑换码
     * @param request
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/updateExchangeCodeDelete")
    public DataResponse updateExchangeCodeDelete(HttpServletRequest request,
                                                 @RequestParam MultipartFile file) throws IOException {
        JSONObject jsonObject = new JSONObject();
        try {
            ExcelUtil excelUtil = new ExcelUtil();
            List<RechargeCodeImportVO> list = excelUtil.excelImport(file.getInputStream(), RechargeCodeImportVO.class);//1.读取前端的excel文件并转换成list集合
            if (list == null || list.size() == 0) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
            memberShipCardService.updateExchangeCodeDelete(list);

        } catch (Exception e) {
            log.error("memberShipCard updateExchangeCodeDelete:", e);
        }
        //返回导入数量、重复数量
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),jsonObject);
    }

    /***
     * 补全会员卡开始日期和结束时间
     */
    @PostMapping("/updateCardDate")
    public DataResponse updateCardDate(){
        memberShipCardService.updateCardDate();
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }



}
